package com.zeroup.auto.assists.step.dy;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.view.accessibility.AccessibilityNodeInfo;

import androidx.annotation.NonNull;

import com.ven.assists.Assists;
import com.ven.assists.stepper.Step;
import com.ven.assists.stepper.StepCollector;
import com.zeroup.auto.assists.DouYinBaseStep;
import com.zeroup.auto.bean.MGanVideo;
import com.zeroup.auto.utils.ApplicationUtiles;

import java.util.List;

public class ShareVideoStep extends DouYinBaseStep {


    private static String jz_packgage = "com.zeroup.matrix";



    @Override
    public void onImpl(@NonNull StepCollector stepCollector) {
        MGanVideo video = ApplicationUtiles.get();
        stepCollector.next(1,false,(i,c)->{//打开矩阵app
            // 获取PackageManager实例
            PackageManager packageManager = Assists.getService().getPackageManager();
            // 创建一个Intent，用于启动应用
            Intent intent = packageManager.getLaunchIntentForPackage(jz_packgage);
            assert Assists.getService() != null;
            Assists.getService().startActivity(intent);
            return  Step.Companion.get(2,this.getClass(),i.getData(),2000);
        }).next(2,false,(i,c)->{//判断是否登录
            List<AccessibilityNodeInfo> isZhibo = Assists.INSTANCE.findById("com.zeroup.matrix:id/downloadVideo");
            if(isZhibo.isEmpty()){
                return  Step.Companion.get(3,this.getClass(),i.getData(),0);//未登录 进入登录
            }
            return  Step.Companion.get(5,this.getClass(),i.getData(),1000);//已登录 进入下载视频
        }).next(3,false,(i,c)->{//矩阵app登录
            List<AccessibilityNodeInfo> isLogin = Assists.INSTANCE.findById("com.zeroup.matrix:id/dy_login");
            if(isLogin.isEmpty()){
                return  Step.Companion.get(1,this.getClass(),i.getData(),0);//未找到按钮，启动app
            }
            isLogin.stream().findFirst().get().performAction(AccessibilityNodeInfo.ACTION_CLICK);//点击登录按钮
            return  Step.Companion.get(4,this.getClass(),i.getData(),3000);//
        }).next(4,false,(i,c)->{//抖音同意授权
            List<AccessibilityNodeInfo> isLogin = Assists.INSTANCE.findByText("同意授权");
            if(isLogin.isEmpty()){
                return  Step.Companion.get(2,this.getClass(),i.getData(),0);//未找到按钮，进入第三步
            }
            isLogin.stream().findFirst().get().performAction(AccessibilityNodeInfo.ACTION_CLICK);//点击同意授权按钮
            return  Step.Companion.get(5,this.getClass(),i.getData(),3000);//
        }).next(5,false,(i,c)->{//点击下载视频
            List<AccessibilityNodeInfo> isZhibo = Assists.INSTANCE.findById("com.zeroup.matrix:id/shareVideo");
            if(isZhibo.isEmpty()){
                return  Step.Companion.get(3,this.getClass(),i.getData(),0);//未登录 进入登录
            }
            isZhibo.stream().findFirst().get().performAction(AccessibilityNodeInfo.ACTION_CLICK);//点击下载按钮
            return  Step.Companion.get(6,this.getClass(),i.getData(),5000);//进入检测下载步骤
        }).next(6,false,(i,c)->{//点击发布视频
            List<AccessibilityNodeInfo> isZhibo = Assists.INSTANCE.findById("com.ss.android.ugc.aweme:id/s_-");
            if(isZhibo.isEmpty()){
                return  Step.Companion.getRepeat();
            }
            isZhibo.forEach(it->{
               it.performAction(AccessibilityNodeInfo.ACTION_CLICK);//点击下载按钮
            });
            return  Step.Companion.getNone();
        });
    }
}
